package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {


    @Autowired
   private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {

        //新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.addSetmeal(setmeal);

        Long id = setmeal.getId();
        //新增菜品套餐关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.addSetmealDish(setmealDishes);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmealList = setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<SetmealVO> setmealPage = (Page<SetmealVO>) setmealList;
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSetmeal(List<Long> ids) {
        //查看套餐是否起售
        for (Long id : ids) {
         SetmealVO setmealVO = setmealMapper.qurrySetmealById(id);
         if(setmealVO.getStatus() == StatusConstant.ENABLE){
             //起售中，不能删除
             throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
         }
        }
        //删除套餐表中的数据
        setmealMapper.deleteSetmeal(ids);
        //删除套餐菜品关系表中的数据
        setmealDishMapper.deleteBySetmealId(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO querySetmealById(Long id) {
        //查询套餐
        SetmealVO setmealVO = setmealMapper.qurrySetmealById(id);
        //查询套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDishMapper.querySetmealDish(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);
        //修改套餐菜品关系
        //删除原有套餐菜品关系表中的数据
        Long dishDTOId = setmealDTO.getId();
        List<Long> dishFlavorId= new ArrayList<>();
        dishFlavorId.add(dishDTOId);
       setmealDishMapper.deleteBySetmealId(dishFlavorId);
        //添加新的套餐菜品关系数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()){
            for (SetmealDish setmealDish : setmealDishes) {
                //将套餐菜品绑在套餐上
                setmealDish.setSetmealId(dishDTOId);
            }
            setmealDishMapper.addSetmealDish(setmealDishes);
        }
    }

    /**
     * 修改套餐起售停售
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.updateSetmeal(setmeal);
    }



    /**
     * 客户端 根据分类id ，套餐状态查询套餐
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 客户端根据套餐id查询套餐中包含的菜品
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
